package com.dayu.recovery.service.impl;

import java.util.List;

import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.WarehouseLogs;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import com.dayu.recovery.mapper.WarehouseLogsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.TransferGoodsMapper;
import com.dayu.recovery.domain.TransferGoods;
import com.dayu.recovery.service.ITransferGoodsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 移交商品详情Service业务层处理
 * 
 * @author HK
 * @date 2023-12-17
 */
@Service
public class TransferGoodsServiceImpl implements ITransferGoodsService 
{
    @Resource
    private TransferGoodsMapper transferGoodsMapper;

    @Resource
    private WarehouseLogsMapper warehouseLogsMapper;

    @Resource
    private RecoveryGoodsMapper recoveryGoodsMapper;


    /**
     * 查询移交商品详情列表
     * 
     * @param transferGoods 移交商品详情
     * @return 移交商品详情
     */
    @Override
    public List<TransferGoods> selectTransferGoodsList(TransferGoods transferGoods)
    {
        return transferGoodsMapper.selectTransferGoodsList(transferGoods);
    }

    /**
     * 删除移交商品详情信息
     * 
     * @param id 移交商品详情ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTransferGoodsById(Long id)
    {
        TransferGoods entity = transferGoodsMapper.queryTransferGoods(id);
        if(ObjectUtils.isEmpty(entity))
            throw new RuntimeException("没有获取到数据");
        WarehouseLogs logs = warehouseLogsMapper.selectWarehouseLogsById(entity.getWarehouseLogsId());
        if(ObjectUtils.isEmpty(logs))
            throw new RuntimeException("没有获取到数据");
        if(logs.getStatus() != 1)
            throw new RuntimeException("已确定，不可删除！");

        RecoveryGoods goods = new RecoveryGoods();
        goods.setGoodsId(entity.getGoodsId());
        goods.setConfirmStatus(1);
        recoveryGoodsMapper.updateRecoveryGoods(goods);
        return transferGoodsMapper.deleteTransferGoodsById(id);
    }
}
